package com.itheima.controller;

import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/15 11:25
 * @Desc: 购物车管理
 */
@RestController
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车列表
     *
     * @return
     */
    @GetMapping("/shoppingCart/list")
    public R<List<ShoppingCart>> listAll(HttpServletRequest request) {
        List<ShoppingCart> shoppingCarts = shoppingCartService.listAll(request);

        return shoppingCarts != null ? R.success(shoppingCarts) : R.error("未查询到任何信息");
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/shoppingCart/add")
    public R<String> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.addShoppingCart(shoppingCart);

        return R.success("添加成功");
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/shoppingCart/clean")
    public R<String> clearShoppingCart() {
        shoppingCartService.clearShoppingCart();

        return R.success("购物车已清空");
    }

    /**
     * 删除购物车单条数据
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/shoppingCart/sub")
    public R<String> deleteShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.subShoppingCart(shoppingCart);

        return R.success("删除成功");
    }
}
